package cn.com.infcn.monitor.task.jvm;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.com.infcn.monitor.bean.IFCJVMThread;
import cn.com.infcn.monitor.bean.IFCJVMThreadInfo;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * Java 虚拟机线程系统的管理接口。
 * 
 * @author jjs
 *
 */
public class JVMThreadTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(JVMThreadTask.class);

	@Override
	public Object execute() throws Exception {
		IFCJVMThread result = jvmThread();
		return result;
	}

	public IFCJVMThread jvmThread() {
		IFCJVMThread ifcThread = null;

		try {

			// 获取ThreadMXBean对象
			ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();

			ifcThread = new IFCJVMThread();

			IFCBeanUtils.copyProperties(threadBean, ifcThread);
			
			ifcThread.setFindDeadlockedThreads(threadBean.findDeadlockedThreads());
			ifcThread.setFindMonitorDeadlockedThreads(threadBean.findMonitorDeadlockedThreads());

			Map<String, IFCJVMThreadInfo> ifcThreadInfoMap = new HashMap<String, IFCJVMThreadInfo>();

			long[] ids = threadBean.getAllThreadIds();
			for (long id : ids) {
				ThreadInfo threadInfo = threadBean.getThreadInfo(id);
				IFCJVMThreadInfo ifcThreadInfo = new IFCJVMThreadInfo();
				IFCBeanUtils.copyProperties(threadInfo, ifcThreadInfo);
				ifcThreadInfo.setThreadCpuTime(threadBean.getThreadCpuTime(id));
				ifcThreadInfo.setThreadUserTime(threadBean.getThreadUserTime(id));
				ifcThreadInfoMap.put(id + "", ifcThreadInfo);
			}
			IFCBeanUtils.copyProperties(threadBean, ifcThread);
			ifcThread.setThreadInfoMap(ifcThreadInfoMap);


			return ifcThread;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}
		return null;
	}
	
	/*private void copyProperties1(ThreadMXBean threadBean, IFCJVMThread ifcThread){
		ifcThread.setAllThreadIds(threadBean.getAllThreadIds());
		ifcThread.setCurrentThreadCpuTime(threadBean.getCurrentThreadCpuTime());
		ifcThread.setCurrentThreadUserTime(threadBean.getCurrentThreadUserTime());
		ifcThread.setDaemonThreadCount(threadBean.getDaemonThreadCount());
		ifcThread.setIsCurrentThreadCpuTimeSupported(threadBean.isCurrentThreadCpuTimeSupported());
		ifcThread.setIsObjectMonitorUsageSupported(threadBean.isObjectMonitorUsageSupported());
		ifcThread.setIsSynchronizerUsageSupported(threadBean.isSynchronizerUsageSupported());
		ifcThread.setIsThreadContentionMonitoringEnabled(threadBean.isThreadContentionMonitoringEnabled());
		ifcThread.setIsThreadContentionMonitoringSupported(threadBean.isThreadContentionMonitoringSupported());
		ifcThread.setIsThreadCpuTimeEnabled(threadBean.isThreadCpuTimeEnabled());
		ifcThread.setIsThreadCpuTimeSupported(threadBean.isThreadCpuTimeSupported());
		ifcThread.setPeakThreadCount(threadBean.getPeakThreadCount());
		ifcThread.setThreadCount(threadBean.getThreadCount());
		ifcThread.setTotalStartedThreadCount(threadBean.getTotalStartedThreadCount());
		
		ifcThread.setFindDeadlockedThreads(threadBean.findDeadlockedThreads());
		ifcThread.setFindMonitorDeadlockedThreads(threadBean.findMonitorDeadlockedThreads());
		
	}
	
	private void copyProperties2(ThreadInfo threadInfo, IFCJVMThreadInfo ifcThreadInfo){
		ifcThreadInfo.setBlockedCount(threadInfo.getBlockedCount());
		ifcThreadInfo.setBlockedTime(threadInfo.getBlockedTime());
		ifcThreadInfo.setInNative(threadInfo.isInNative());
		ifcThreadInfo.setLockName(threadInfo.getLockName());
		ifcThreadInfo.setLockOwnerId(threadInfo.getLockOwnerId());
		ifcThreadInfo.setLockOwnerName(threadInfo.getLockOwnerName());
		ifcThreadInfo.setSuspended(threadInfo.isSuspended());
		ifcThreadInfo.setThreadId(threadInfo.getThreadId());
		ifcThreadInfo.setThreadName(threadInfo.getThreadName());
		ifcThreadInfo.setThreadState(threadInfo.getThreadState());
		ifcThreadInfo.setWaitedCount(threadInfo.getWaitedCount());
		ifcThreadInfo.setWaitedTime(threadInfo.getWaitedTime());
	}*/

	public static void main(String[] args) {
		JVMThreadTask task = new JVMThreadTask();
		IFCJVMThread result = task.jvmThread();
		
		System.out.println(result);
	}

}
